local addon, db = ...

local autobuy = {}

local Initialize_AutoBuy
local class = select(2, UnitClass"player")

local ONE_SPEC_WITH_GLYPH_IS_ENOUGH = false

local hasGlyph(spellId, talentGroup)
	for i = 1, 6 do
		local enabled, _, spell = GetGlyphSocketInfo(i, talentGroup)
		if spell == spellId then return true end
	end
end

local missingGlyph
if ONE_SPEC_WITH_GLYPH_IS_ENOUGH then
	missingGlyph = function(spellId)
		for i = 1, GetNumTalentGroups() do
			if hasGlyph(spellId, i) then
				return false
			end
		end
		return true
	end
else
	missingGlyph = function(spellId)
		for i = 1, GetNumTalentGroups() do
			if not hasGlyph(spellId, i) then
				return true
			end
		end
	end
end

local check_Glyphs = false

if class == "DRUID" then
	check_Glyphs = true
	Initialize_AutoBuy = function (level)
		-- Gift of the wild
		if level >= 80 then
			autobuy[44605] = 1
		elseif level >= 70 then
			autobuy[22148] = 1
		elseif level >= 60 then
			autobuy[17026] = 1
		elseif level >= 50 then
			autobuy[17021] = 1
		end
		-- Rebirth
		if missingGlyph(58161) then
			if level >= 79 then
				autobuy[44614] = 1
			elseif level >= 69 then
				autobuy[22147] = 1
			elseif level >= 60 then
				autobuy[17038] = 1
			elseif level >= 50 then
				autobuy[17037] = 1
			elseif level >= 40 then
				autobuy[17036] = 1
			elseif level >= 30 then
				autobuy[17035] = 1
			elseif level >= 20 then
				autobuy[17034] = 1
			end
		end
	end
elseif class == "MAGE" then
	Initialize_AutoBuy = function (level)
		-- Teleportation
		if level >= 20 then
			autobuy[17031] = 1
		end
		-- Portals
		if level >= 35 then
			autobuy[17032] = 1
		end
		-- Arcane Powder
		if level >= 56 then
			autobuy[17020] = 1
		end
	end
elseif class == "PALADIN" then
	Initialize_AutoBuy = function (level)
		-- Symbol of Divinity
		if level >= 30 then
			autobuy[17033] = 1
		end
		-- Symbol of Kings
		if level >= 52 then
			autobuy[21177] = 1
		end
	end
elseif class == "PRIEST" then
	Initialize_AutoBuy = function (level)
		-- Candles
		if level >= 80 then
			autobuy[44615] = 2
		elseif level >= 77 then
			autobuy[44615] = 1
			autobuy[10729] = 1
		elseif level >= 60 then
			autobuy[10729] = 2
		elseif level >= 56 then
			autobuy[10729] = 1
			autobuy[10728] = 1
		elseif level >= 48 then
			autobuy[10728] = 1
		end
	end
elseif class == "SHAMAN" then
	check_Glyphs = true
	Initialize_AutoBuy = function (level)
		-- Ankh
		if missingGlyph(58263) and level >= 30 then
			autobuy[17030] = 1
		end
	end
elseif class == "ROGUE" then
	Initialize_AutoBuy = function (level)
		-- Crippling Poison
		if level >= 20 then
			autobuy[3775] = 1
		end
		-- Mind-numbing Poison
		if level >= 24 then
			autobuy[5237] = 1
		end
		-- Wound Poison
		if level >= 78 then
			autobuy[43235] = 1
		elseif level >= 72 then
			autobuy[43234] = 1
		elseif level >= 64 then
			autobuy[22055] = 1
		elseif level >= 56 then
			autobuy[10922] = 1
		elseif level >= 48 then
			autobuy[10921] = 1
		elseif level >= 40 then
			autobuy[10920] = 1
		elseif level >= 32 then
			autobuy[10918] = 1
		end
		-- Instant Poison
		if level >= 79 then
			autobuy[43231] = 1
		elseif level >= 73 then
			autobuy[43230] = 1
		elseif level >= 68 then
			autobuy[21927] = 1
		elseif level >= 60 then
			autobuy[8928] = 1
		elseif level >= 52 then
			autobuy[8927] = 1
		elseif level >= 44 then
			autobuy[8926] = 1
		elseif level >= 36 then
			autobuy[6950] = 1
		elseif level >= 28 then
			autobuy[6949] = 1
		elseif level >= 20 then
			autobuy[6947] = 1
		end
		-- Deadly Poison
		if level >= 80 then
			autobuy[43233] = 1
		elseif level >= 76 then
			autobuy[43232] = 1
		elseif level >= 70 then
			autobuy[22054] = 1
		elseif level >= 62 then
			autobuy[22053] = 1
		elseif level >= 60 then
			autobuy[20844] = 1
		elseif level >= 54 then
			autobuy[8985] = 1
		elseif level >= 46 then
			autobuy[8984] = 1
		elseif level >= 38 then
			autobuy[2893] = 1
		elseif level >= 30 then
			autobuy[2892] = 1
		end
		-- Anesthetic Poison
		if level >= 77 then
			autobuy[43237] = 1
		elseif level >= 68 then
			autobuy[21835] = 1
		end
	end
elseif class == "DEATHKNIGHT" then
	check_Glyphs = true
	Initialize_AutoBuy = function (level)
		-- Raise Dead
		if missingGlyph(59313) then
			autobuy[37201] = 1
		end
	end
else
	Initialize_AutoBuy = function () end
end

db.Register(function (self)
	for bag = 0, NUM_BAG_SLOTS do
		for slot = 1, GetContainerNumSlots(bag) do
			local link = GetContainerItemLink(bag, slot)
			local quality = link and select(3, GetItemInfo(link))
			if quality == 0 then
				UseContainerItem(bag, slot)
			end
		end
	end
	for item, stacks in pairs(autobuy) do
		local link = select(2, GetItemInfo(item))
		if link then
			local maxStack = select(8, GetItemInfo(item))
			local count = stacks * maxStack
			local have = GetItemCount(link)
			local tobuy = count - have
			if tobuy > 0 then
				for i = 1, GetMerchantNumItems() do
					if GetMerchantItemLink(i) == link then
						local quantity = select(4, GetMerchantItemInfo(i))
						while tobuy > 0 do
							local c = math.min(tobuy, maxStack)
							c = math.floor(c / quantity)
							if c == 0 then break end
							BuyMerchantItem(i, c)
							tobuy = tobuy - (c * quantity)
						end
						break
					end
				end
			end
		end
	end
	return true
end, "MERCHANT_SHOW")

db.Register(function (self)
	Initialize_AutoBuy(UnitLevel"player")
end, "PLAYER_ENTERING_WORLD")

if check_Glyphs then
	db.Register(function (self)
		Initialize_AutoBuy(UnitLevel"player")
		return true
	end, "GLYPH_UPDATED", "GLYPH_ADDED", "GLYPH_REMOVED")
end


db.Register(function (self, _, level)
	wipe(autobuy)
	Initialize_AutoBuy(tonumber(level))
	return true
end, "PLAYER_LEVEL_UP")
